using System;
using System.Windows.Forms;
using Belmost4PDA.CommonForms;
using Belmost4PDA.Constants;

namespace Belmost4PDA.Logic
{
    public class StateView:AbstractState
    {
        public override bool CanDo(AbstractCommand command)
        {
            CommandID id = command.GetID();
            bool flag;
            try
            {
                flag = ((Control_Main) InterfaceDispatcher._instance.TopControls).EmptyGrid;
            }
            catch(Exception)
            {
                flag = false;
            }

            bool Desision = true;
            
            switch(id)
            {
                case CommandID.Post:
                case CommandID.Cancel: Desision = false;
                    break;
                case CommandID.ChangeParam:
                case CommandID.Exit:
                    break;
                case CommandID.Edit:
                    if (WorkedParam == null ||InterfaceDispatcher._instance.TopControls.Editable()==false)
                        
                        Desision = false;
                    Desision = Desision && !flag;
                    break;
                case CommandID.Insert:
                    if (InterfaceDispatcher._instance.TopControls.GetGrid() != null)
                        break;
                    else
                    {
                        Desision = false;
                        break;
                    }
                case CommandID.Delete: if (InterfaceDispatcher._instance.TopControls.GetGrid() != null && !flag)
                    break;
                else 
                    {
                        Desision = false;
                        break;
                    }

            }
           
            return Desision && base.CanDo(command);
        }

        public override bool AskToDo(AbstractCommand command)
        {
            CommandID id = command.GetID();
            switch(id)
            {
                case CommandID.Delete: 
                    if(dialog.CommitDelete()) return true;
                    else return false;
                default:
                    return true;
            }
        }

        public override void SetupControls(Form form)
        {
            bool Empty = ((Control_Main) InterfaceDispatcher._instance.TopControls).EmptyGrid;
            foreach (Control o in form.Controls)
            {
                IReadOnly check = o as IReadOnly;
                if (Empty && o.GetType() == typeof(ListView)) 
                {
                    o.Enabled = false;
                }
                else
                    if (check!=null)
                    check.ROnly = true;
                else o.Enabled = true;
            };

            if (form.Menu != null) CheckMenu(form.Menu);
        }
         
    }
}
